package com.gugee.ins.data.blogger.statistics.core;


import com.gugee.common.thread.ScheduleTemplate;
import com.gugee.ins.common.dal.service.blogger.InsBloggerDao;
import com.gugee.ins.common.dal.service.blogger.InsBloggerAddDao;
import com.gugee.ins.common.dal.service.blogger.InsBloggerMainDao;
import com.gugee.ins.common.model.mysql.blogger.InsBlogger;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.data.blogger.statistics.config.BloggerStatisticsAppConfig;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.ForkJoinPool;

@Slf4j
public abstract class StBaseService {

    /**
     * 线程调度
     */
    @Autowired
    protected ScheduleTemplate scheduleTemplate;

    @Autowired
    protected InsBloggerAddDao insBloggerAddDao;

    @Autowired
    protected InsBloggerMainDao insBloggerMainDao;

    @Autowired
    protected InsBloggerDao insBloggerDao;

    @Autowired
    protected BloggerStatisticsAppConfig bloggerStatisticsAppConfig;

    protected String workName;

    protected ForkJoinPool forkJoinPool = ThreadPoolUtils.forkJoinPool(10,"defaultThreadPool");



    public void handler(List<InsBloggerMessage> insBloggerMessages){
        try{
            forkJoinPool.submit(()->insBloggerMessages.parallelStream().forEach(insBloggerMessage -> {
                try{
                    statistics(insBloggerMessage);
                }catch (Exception e){
                    log.error("{} error! uid:{}",workName,insBloggerMessage.getUid(),e);
                }
            })).get();
        }catch (Exception e){
            log.error("{} error!",workName,e);
        }

    }

    protected InsBlogger blogger(long uid){
        return insBloggerDao.findByUid(uid);
    }

    protected InsBloggerMain bloggerMain(long uid){
        return insBloggerMainDao.findByUid(uid);
    }

    protected abstract void statistics(InsBloggerMessage insBloggerMessage);
}
